package com.radar.controller;

import cn.hutool.core.io.FileUtil;
import com.radar.model.ResultBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.nio.charset.Charset;
import java.util.Map;

@Api(tags = "预警配置 controller")
@RestController
@RequestMapping("config")
public class ConfigController {

    @Value("${radar.process-path}")
    private String configPath;

    @ApiOperation(value = "预警配置接口", httpMethod = "GET", notes = "获取风廓线,以及色斑图")
    @GetMapping("/infoOrUpdate")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "config", value = "", example = "", defaultValue = "", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "station", value = "", example = "", defaultValue = "", paramType = "query", dataType = "String")
    })
    public ResultBody infoOrUpdate(String config, String station) {
        String file = configPath + File.separator + "setting" +File.separator + station + ".txt";
        if (!FileUtil.exist(file)) {
            FileUtil.touch(file);
        }
        String c = FileUtil.readString(file, Charset.defaultCharset());
        if (config == null || "".equals(config)) {

        } else {
            c = config;
            FileUtil.writeString(c, new File(file), Charset.defaultCharset());
        }
        return ResultBody.ok().data(c);
    }

    @ApiOperation(value = "预警配置接口", httpMethod = "GET", notes = "获取风廓线,以及色斑图")
    @PostMapping("/infoOrUpdatePost")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "config", value = "", example = "", defaultValue = "", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "station", value = "", example = "", defaultValue = "", paramType = "query", dataType = "String")
    })
    public ResultBody infoOrUpdatePost(@RequestBody Map<String, String> configJson) {
        String station = configJson.get("station");
        String config = configJson.get("config");
        String file = configPath + File.separator + station + ".txt";
        if (!FileUtil.exist(file)) {
            FileUtil.touch(file);
        }
        String c = FileUtil.readString(file, Charset.defaultCharset());
        if (config == null || "".equals(config)) {

        } else {
            c = config;
            FileUtil.writeString(c, new File(file), Charset.defaultCharset());
        }
        return ResultBody.ok().data(c);
    }
}
